<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="ujn" uri="http://ujn.edu.cn/common/" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName()
            + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>发票管理</title>
    <!-- 引入css样式文件 -->
    <!-- Bootstrap Core CSS -->
    <link href="<%=basePath%>css/bootstrap.min.css" rel="stylesheet"/>
    <!-- MetisMenu CSS -->
    <link href="<%=basePath%>css/metisMenu.min.css" rel="stylesheet"/>
    <!-- DataTables CSS -->
    <link href="<%=basePath%>css/dataTables.bootstrap.css" rel="stylesheet"/>
    <!-- Custom CSS -->
    <link href="<%=basePath%>css/sb-admin-2.css" rel="stylesheet"/>
    <!-- Custom Fonts -->
    <link href="<%=basePath%>css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
    <link href="<%=basePath%>css/boot-crm.css" rel="stylesheet" type="text/css"/>
    <link href="<%=basePath%>css/bootstrap-datetimepicker.css" type="text/css">
</head>
<body>
<div id="wrapper">
    <jsp:include page="menu.jsp"/>
    <!-- 发票列表查询部分  start-->
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">发票管理</h1>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
        <div class="panel panel-default">
            <div class="panel-body">
                <form class="form-inline" method="get"
                      action="${pageContext.request.contextPath }/invoice/list.action">
                    <div class="form-group">
                        <label for="orderId">订单编号</label>
                        <input type="text" class="form-control" id="orderId"
                               value="${ orderId }" name="orderId"/>
                    </div>

                    <div class="form-group">
                        <label for="invoiceTitle">购物方</label>
                        <input type="text" class="form-control" id="invoiceTitle"
                               value="${invoiceTitle }" name="invoiceTitle"/>
                    </div>


                    <div class="form-group">
                        <label for="invoiceType">发票类型</label>
                        <select class="form-control" id="invoiceType" name="invoiceType">
                            <option value="">--请选择--</option>


                            <c:forEach items="${dictByTypeCode}" var="item">
                                <option value="${item.dict_id}"
                                        <c:if test="${item.dict_id == invoiceType}"> selected</c:if>>
                                        ${item.dict_item_name }
                                </option>
                            </c:forEach>

                        </select>
                    </div>


                    <button type="submit" class="btn btn-primary">查询</button>
                </form>
            </div>
        </div>
        <a href="#" class="btn btn-primary" data-toggle="modal"
           data-target="#newInvoiceDialog" onclick="clearInvoice()">新建</a>
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">发票信息列表</div>
                    <!-- /.panel-heading -->
                    <table class="table table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>发票id</th>
                            <th>订单编号</th>
                            <th>购物方</th>
                            <th>发票内容</th>
                            <th>发票金额</th>
                            <th>发票类型</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        <c:forEach items="${page.rows}" var="row">
                            <tr>
                                <td>${row.invoiceId}</td>
                                <td>${row.orderId}</td>
                                <td>${row.invoiceTitle}</td>
                                <td>${row.invoiceContent}</td>
                                <td>${row.invoiceAmount}</td>
                                <td>
                                    <c:forEach items="${dictByTypeCode}" var="item">
                                        <c:if test="${item.dict_id==row.invoiceType}">  ${item.dict_item_name }</c:if>
                                    </c:forEach>
                                </td>
                                <td>
                                    <a href="#" class="btn btn-primary btn-xs" data-toggle="modal"
                                       data-target="#invoiceEditDialog" onclick="editInvoice(${row.invoiceId})">修改</a>
                                    <a href="#" class="btn btn-danger btn-xs"
                                       onclick="deleteInvoice(${row.invoiceId})">删除</a>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                    <div class="col-md-12 text-right">
                        <ujn:page url="${pageContext.request.contextPath }/user/list.action"/>
                    </div>
                    <!-- /.panel-body -->
                </div>
                <!-- /.panel -->
            </div>
            <!-- /.col-lg-12 -->
        </div>
    </div>
    <!-- 发票列表查询部分  end-->
</div>
<!-- 创建发票模态框 -->
<div class="modal fade" id="newInvoiceDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">新建发票信息</h4>
            </div>
            <form class="form-horizontal" id="new_invoice_form">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="new_orderId" class="col-sm-2 control-label">
                            订单编号
                        </label>
                        <div class="col-sm-10">
                            <input type="number" class="form-control" id="new_orderId" placeholder="订单编号"
                                   name="orderId"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="new_invoiceTitle" class="col-sm-2 control-label">
                            购物方
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="new_invoiceTitle" placeholder="购物方"
                                   name="invoiceTitle"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="new_invoiceContent" class="col-sm-2 control-label">
                            发票内容
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="new_invoiceContent" placeholder="发票内容"
                                   name="invoiceContent"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="new_invoiceAmount" class="col-sm-2 control-label">
                            发票金额
                        </label>
                        <div class="col-sm-10">
                            <input type="text"
                                   onkeypress="if (event.keyCode!=46 && event.keyCode!=45 && event.keyCode<48 || event.keyCode>57) event.returnValue=false"
                                   class="form-control" id="new_invoiceAmount" placeholder="发票金额"
                                   name="invoiceAmount"/>
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="new_invoiceType" style="float:left;padding:7px 15px 0 27px;">发票类型</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="new_invoiceType" name="invoiceType">
                                <option value="">--请选择--</option>
                                <c:forEach items="${dictByTypeCode}" var="item">
                                    <option value="${item.dict_id}">
                                            ${item.dict_item_name }
                                    </option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" class="btn btn-primary" onclick="createInvoice()">创建发票</button>
        </div>
    </div>
</div>
<!-- 修改发票模态框 -->
<div class="modal fade" id="invoiceEditDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">修改发票信息</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="edit_invoice_form">
                    <input type="hidden" id="edit_invoiceId" name="invoiceId"/>

                    <div class="form-group">
                        <label for="edit_orderId" class="col-sm-2 control-label">
                            订单编号
                        </label>
                        <div class="col-sm-10">
                            <input type="number" class="form-control" id="edit_orderId" placeholder="订单编号"
                                   name="orderId"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="edit_invoiceTitle" class="col-sm-2 control-label">
                            购物方
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_invoiceTitle" placeholder="购物方"
                                   name="invoiceTitle"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit_invoiceContent" class="col-sm-2 control-label">
                            发票内容
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_invoiceContent" placeholder="发票内容"
                                   name="invoiceContent"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit_invoiceAmount" class="col-sm-2 control-label">
                            发票金额
                        </label>
                        <div class="col-sm-10">
                            <input type="text"
                                   onkeypress="if (event.keyCode!=46 && event.keyCode!=45 && event.keyCode<48 || event.keyCode>57) event.returnValue=false"
                                   class="form-control" id="edit_invoiceAmount" placeholder="发票金额"
                                   name="invoiceAmount"/>
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="edit_invoiceType" style="float:left;padding:7px 15px 0 27px;">发票类型</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="edit_invoiceType" name="invoiceType">
                                <option value="">--请选择--</option>
                                <c:forEach items="${dictByTypeCode}" var="item">
                                    <option value="${item.dict_id}"
                                            <c:if test="${item.dict_id == invoiceType}"> selected</c:if>>
                                            ${item.dict_item_name }
                                    </option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>


                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="updateInvoice()">保存修改</button>
            </div>
        </div>
    </div>
</div>
<!-- 引入js文件 -->
<!-- jQuery -->
<script src="<%=basePath%>js/jquery-1.11.3.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="<%=basePath%>js/bootstrap.min.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="<%=basePath%>js/metisMenu.min.js"></script>
<!-- DataTables JavaScript -->
<script src="<%=basePath%>js/jquery.dataTables.min.js"></script>
<script src="<%=basePath%>js/dataTables.bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="<%=basePath%>js/sb-admin-2.js"></script>
<script src="<%=basePath%>js/moment.min.js"></script>
<script src="<%=basePath%>js/bootstrap-datetimepicker.min.js"></script>
<!-- 编写js代码 -->
<script type="text/javascript">

    //清空新建发票窗口中的数据
    function clearInvoice() {
        $("#new_orderId").val("");
        $("#new_invoiceTitle").val("")
        $("#new_invoiceContent").val("")
        $("#new_invoiceAmount").val("");
        $("#new_invoiceType").val("");
    }

    // 创建
    function createInvoice() {
        $.post("<%=basePath%>invoice/create.action",
            $("#new_invoice_form").serialize(), function (data) {
                if (data == "OK") {
                    alert("发票创建成功！");
                    window.location.reload();
                } else if (data == "fail1"){
                    alert("请输入正确的订单编号");
                    window.location.reload();
                }else if(data == "fail2"){
                    alert("该订单已存在发票记录，无法创建");
                    window.location.reload();
                }else if(data == "fail3"){
                    alert("未找到该订单号，无法创建");
                    window.location.reload();
                }
                else {
                    alert("未知错误,创建失败");
                    window.location.reload();
                }
            });
    }
    
    // 通过id获取修改的信息
    function editInvoice(id) {
        $.ajax({
            type: "get",
            url: "<%=basePath%>invoice/getInvoiceById.action",
            data: {"id": id},
            success: function (data) {
                $("#edit_invoiceId").val(data.invoiceId);
                $("#edit_orderId").val(data.orderId);
                $("#edit_invoiceTitle").val(data.invoiceTitle)
                $("#edit_invoiceContent").val(data.invoiceContent)
                $("#edit_invoiceAmount").val(data.invoiceAmount)
                $("#edit_invoiceType").val(data.invoiceType)
            }
        });
    }

    // 执行修改操作
    function updateInvoice() {
        $.post("<%=basePath%>invoice/update.action", $("#edit_invoice_form").serialize(), function (data) {
            if (data == "OK") {
                alert("发票修改成功！");
                window.location.reload();
            } else if (data == "fail1"){
                alert("请输入正确的订单编号");
                window.location.reload();
            }else if(data == "fail3"){
                alert("未找到该订单号，无法修改");
                window.location.reload();
            }
            else {
                alert("未知错误,修改失败");
                window.location.reload();
            }
        });
    }

    // 删除
    function deleteInvoice(id) {
        if (confirm('确实要删除该发票吗?')) {
            $.post("<%=basePath%>invoice/delete.action", {"id": id},
                function (data) {
                    if (data == "OK") {
                        alert("发票删除成功！");
                        window.location.reload();
                    } else {
                        alert("删除发票失败！");
                        window.location.reload();
                    }
                });
        }
    }

</script>
</body>
</html>